package cn.yeyiu.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import androidx.core.content.FileProvider;

import cn.yeyiu.util.bean.BlogInfo;
import cn.yeyiu.util.bean.MediaData;
import cn.yeyiu.util.bean.MangaEP;
import cn.yeyiu.util.bean.MangaDetail;
import cn.yeyiu.util.bean.TimeVideoInfo;
import cn.yeyiu.util.bean.VideoDetail;
import cn.yeyiu.util.bean.VideoInfo;

import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class MediaUtil {
    public static void readBlog(Context context, Class<?> blogClass, BlogInfo blogInfo) {
        Intent intent = new Intent(context, blogClass);
        intent.putExtra("blog_info", JSONObject.toJSONString(blogInfo));
        context.startActivity(intent);
    }

    public static void playVideo(Context context, Class<?> videoClass, VideoInfo videoInfo) {
        ExternalJSONFile.saveDatas(context, ExternalJSONFile.recent, new TimeVideoInfo(videoInfo));
        Intent intent = new Intent(context, videoClass);
        intent.putExtra("video_info", JSONObject.toJSONString(videoInfo));
        context.startActivity(intent);
    }

    public static void viewVideoDetail(Context context, Class<?> videoClass, VideoDetail videoDetail) {
        Intent intent = new Intent(context, videoClass);
        intent.putExtra("video_detail", JSONObject.toJSONString(videoDetail));
        context.startActivity(intent);
    }

    public static void readManga(Context context, Class<?> mangaClass, MangaDetail mangaDetail) {
        Intent intent = new Intent(context, mangaClass);
        intent.putExtra("mangaDetail", JSONObject.toJSONString(mangaDetail));
        context.startActivity(intent);
    }

    public static void readMangaEp(Context context, Class<?> mangaViewActivity, MangaEP mangaEP) {
        Intent intent = new Intent(context, mangaViewActivity);
        intent.putExtra("mangaEP", JSONObject.toJSONString(mangaEP));
        context.startActivity(intent);
    }

    public static void playLocalVideo(Context context, File videoFile) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri;
        if (Build.VERSION.SDK_INT >= 24) {
            uri = FileProvider.getUriForFile(context, "cn.yeyiu", videoFile);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            uri = Uri.fromFile(videoFile);
        }
        intent.setDataAndType(uri, "video/*");
        context.startActivity(intent);
    }


    private static String addHost(String str) {
        return NetConfig.getHost() + str;
    }

    private static String encodeUrl(String str) {
        if (str == null || "".equals(str)) {
            return "";
        } else {
            try {
                String str1 = new String(str.getBytes(StandardCharsets.UTF_8));
                return URLEncoder.encode(str1, "UTF-8")
                        .replace("+", "%20");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static void fixLocalVideoInfo(VideoInfo videoInfo) {
        if (videoInfo != null && !videoInfo.videoSrc.startsWith("http")) {
            videoInfo.videoSrc = addHost(videoInfo.videoSrc);
            videoInfo.picSrc = addHost(videoInfo.picSrc);
        }
    }

    public static void fixLocalMangaDetail(MangaDetail mangaDetail) {
        if (mangaDetail != null && !mangaDetail.getLogo().startsWith("http")) {
            mangaDetail.setLogo(addHost(mangaDetail.getLogo()));
            for (MangaEP ep : mangaDetail.getEps()) {
                ep.setLogo(addHost(ep.getLogo()));

                List<String> newPicList = new ArrayList<>();
                for (String url : ep.picList) {
                    newPicList.add(addHost(url));
                }
                ep.picList = newPicList;
            }
        }
    }

}
